/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.commons.base.query;

/**
 * 说明：
 * 分页算法
 * @Author：李智深
 * @Date：2011-11-10
 */
public class PageIndex {
	
	/** 开始索引 */
	private long startindex;
	
	/** 结束索引 */
	private long endindex;
	
	public PageIndex(long startindex, long endindex) {
		this.startindex = startindex;
		this.endindex = endindex;
	}
	
	public long getStartindex() {
		return startindex;
	}
	public void setStartindex(long startindex) {
		this.startindex = startindex;
	}
	public long getEndindex() {
		return endindex;
	}
	public void setEndindex(long endindex) {
		this.endindex = endindex;
	}
	
	/**
	 * 
	 * @param pageCode 页面代码，显示多少页
	 * @param currentPage 当前页
	 * @param totalPage 总页数
	 * @return
	 */
	public static PageIndex getPageIndex(long pageCode, int currentPage, long totalPage){
			long startpage = currentPage-(pageCode%2==0? pageCode/2-1 : pageCode/2);
			long endpage = currentPage+pageCode/2;
			if(startpage<1){
				startpage = 1;
				if(totalPage>=pageCode) endpage = pageCode;
				else endpage = totalPage;
			}
			if(endpage>totalPage){
				endpage = totalPage;
				if((endpage-pageCode)>0) startpage = endpage-pageCode+1;
				else startpage = 1;
			}
			return new PageIndex(startpage, endpage);		
	}
	
	public static void main(String[] args) {
		PageIndex p = getPageIndex(9,0,30) ;
		for (long i = p.getStartindex(); i <= p.getEndindex(); i++) {
			System.out.print("【"+i+"】");
		}
	}
	
	
	
}
